using Unity.Entities;
using Unity.Mathematics;

namespace Game.Core
{
    public partial struct DamageableRefreshJob : IJobEntity
    {
        public float DeltaTime;
        void Execute(ref Damageable damageable, ref PropertyData data)
        {
            if ((data.Refresh & PropertyRefresh.Base) == PropertyRefresh.Base ||
                (data.Refresh & PropertyRefresh.Damageable) == PropertyRefresh.Damageable)
            {
                PropertyUtil.RefreshDamageable(ref damageable, in data); 
            }
            
            if ((data.Refresh & PropertyRefresh.Init) == PropertyRefresh.Init)
            {
                damageable.Detail.CurHealthPoint = damageable.Detail.MaxHealthPoint;
            }

            if (damageable.Detail.CurHealthPoint > 0f)
            {
                damageable.Detail.CurHealthPoint =
                    math.min(damageable.Detail.CurHealthPoint + damageable.Detail.HealthRecover * DeltaTime,
                        damageable.Detail.MaxHealthPoint);
            }
            else
            {
                damageable.Detail.CurHealthPoint = 0f;
            }

            if (data.Refresh == PropertyRefresh.Damageable)
                data.Refresh = PropertyRefresh.None;
        }
    }
}